import { StationModel } from '@/models/station.model';
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/sequelize';

@Injectable()
export class StationService {
  constructor(
    @InjectModel(StationModel)
    private readonly stationModel: typeof StationModel,
  ) {}
  async getStations() {
    const { rows, count } = await this.stationModel.findAndCountAll({
      attributes: { exclude: ['created_time', 'updated_time'] },
      order: ['id'],
    });
    const list = [];
    for (let i = 0; i < rows.length; i++) {
      if (rows[i].parentId === 0) {
        list.push({
          ...rows[i].dataValues,
          children: [],
        });
      } else {
        const p = list.find((l) => l.id === rows[i].parentId);
        p.children.push({
          ...rows[i].dataValues,
        });
      }
    }
    return { total: count, list };
  }
}
